\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\input{../labStyle}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}




\license{This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported (\web{http://creativecommons.org/licenses/by-sa/3.0/}). This lab was adapted for OpenIntro by Andrew Bray and Mine \c{C}etinkaya-Rundel from a lab written by the faculty and TAs of UCLA Statistics.}

\section*{Lab 2: Probability}

\subsection*{Hot Hands}
Basketball players who make several baskets in succession are described as having a ``hot hand''. Fans and players have long believed in the hot hand phenomenon, which refutes the assumption that each shot is independent of the next. However, a 1985 paper by Gilovich, Vallone, and Tversky collected evidence that contradicted this belief and showed that successive shots are independent events\symbolfootnote[2]{``The Hot Hand in Basketball: On the Misperception of Random Sequences'', Gilovich, T., Vallone, R., Tversky, A., 1985. Cognitive Psychology, 17, pp. 295-314.}. This paper started a great controversy that continues to this day, as you can see by Googling ``hot hand basketball''.

We do not expect to resolve this controversy today. However, in this lab we'll apply one approach to answering questions like this. The goals for this lab are to (1) think about the effects of independent and dependent events, (2) learn how to simulate shooting streaks in R, and (3) to compare a simulation to actual data in order to determine if the hot hand phenomenon appears to be real.

%

\subsection*{Saving your code}
Click on \menu{File $\rightarrow$ New $\rightarrow$ R Script}. This will open a blank document above the console. As you go along you can copy and paste your code here and save it. This is a good way to keep track of your code and be able to reuse it later. To run your code from this document you can either copy and paste it into the console, highlight the code and hit the \menu{Run} button, or highlight the code and hit \menu{command+enter} or a mac or \menu{control+enter} on a PC.

You'll also want to save this script (code document). To do so click on the disk icon. The first time you hit save, RStudio will ask for a file name; you can name it anything you like. Once you hit save you'll see the file appear under the \menu{Files} tab in the lower right panel. You can reopen this file anytime by simply clicking on it.

%

\subsection*{Getting Started}
Our investigation will focus on the performance of one player: Kobe Bryant of the Los Angeles Lakers. His performance against the Orlando Magic in the 2009 NBA finals earned him the title ``Most Valuable Player'' and many spectators commented on how he appeared to show a hot hand. Let's load some data from those games and look at the first several rows.


\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{download.file}\hlstd{(}\hlstr{"http://www.openintro.org/stat/data/kobe.RData"}\hlstd{,} \hlkwc{destfile} \hlstd{=} \hlstr{"kobe.RData"}\hlstd{)}

\hlkwd{load}\hlstd{(}\hlstr{"kobe.RData"}\hlstd{)}

\hlkwd{head}\hlstd{(kobe)}
\end{alltt}
\end{kframe}
\end{knitrout}


In this data frame, every row records a shot taken by Kobe Bryant. If he hit the shot (made a basket), a hit, \hlstr{H}, is recorded in the column named \hlstd{basket}, otherwise a miss, \hlstr{M}, is recorded.

Just looking at the string of hits and misses, it can be difficult to gauge whether or not it seems like Kobe was shooting with a hot hand. One way we can approach this is by considering the belief that hot hand shooters tend to go on shooting streaks. For this lab, we define the length of a shooting streak to be the \emph{number of consecutive baskets made until a miss occurs}.

For example, in Game 1 Kobe had the following sequence of hits and misses from his nine shot attempts in the first quarter:

\begin{center}
 H \quad M \quad \textbar \quad M \quad \textbar \quad H \quad H \quad M \quad \textbar \quad M \quad \textbar \quad M \quad \textbar \quad M
\end{center}

To verify this use the following command:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{kobe}\hlopt{$}\hlstd{basket[}\hlnum{1}\hlopt{:}\hlnum{9}\hlstd{]}
\end{alltt}
\end{kframe}
\end{knitrout}


Within the nine shot attempts, there are six streaks, which are separated by a ``\textbar'' above. Their lengths are one, zero, two, zero, zero, zero (in order of occurrence).

\begin{exercise}
What does a streak length of 1 mean, i.e. how many hits and misses are in a streak of 1? What about a streak length of 0?
\end{exercise}

The custom function \hlkwd{calc\_streak}, which was loaded in with the data, may be used to calculate the lengths of all shooting streaks and then look at the distribution.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{kobe_streak} \hlkwb{<-} \hlkwd{calc_streak}\hlstd{(kobe}\hlopt{$}\hlstd{basket)}

\hlkwd{barplot}\hlstd{(}\hlkwd{table}\hlstd{(kobe_streak))}
\end{alltt}
\end{kframe}
\end{knitrout}


Note that instead of making a histogram, we chose to make a bar plot from a table of the streak data. A bar plot is preferable here since our variable is discrete -- counts -- instead of continuous.

\begin{exercise}
\label{kobeStreak}
Describe the distribution of Kobe's streak lengths from the 2009 NBA finals. What was his typical streak length? How long was his longest streak of baskets?
\end{exercise}

%

\subsection*{Compared to What?}
We've shown that Kobe had some long shooting streaks, but are they long enough to support the belief that he had hot hands? What can we compare them to?

To answer these questions, let's return to the idea of \emph{independence}. Two processes are independent if the outcome of one process doesn't effect the outcome of the second. If each shot that a player takes is an independent process, having made or missed your first shot will not affect the probability that you will make or miss your second shot.

A shooter with a hot hand will have shots that are \emph{not} independent of one another. Specifically, if the shooter makes his first shot, the hot hand model says he will have a \emph{higher} probability of making his second shot.

Let's suppose for a moment that the hot hand model is valid for Kobe. During his career, the percentage of time Kobe makes a basket (i.e. his shooting percentage) is about 45\%, or in probability notation,

\[ P(\textnormal{shot 1 = H}) = 0.45 \]

If he makes the first shot and has a hot hand (\emph{not} independent shots), then the probability that he makes his second shot would go up to, let's say, 60\%,

\[ P\textnormal{(shot 2 = H} \, | \, \textnormal{shot 1 = H}) = 0.60 \]

As a result of these increased probabilites, you'd expect Kobe to have longer streaks. Compare this to the skeptical perspective where Kobe does \emph{not} have a hot hand, where each shot is independent of the next. If he hit his first shot, the probability that he makes the second is still 0.45.

\[ P(\textnormal{shot 2 = H} \, | \, \textnormal{shot 1 = H}) = 0.45 \]

In other words, making the first shot did nothing to effect the probability that he'd make his second shot. If Kobe's shots are independent, then he'd have the same probability of hitting every shot regardless of his past shots: 45\%.

Now that we've phrased the situation in terms of independent shots, let's return to the question: how do we tell if Kobe's shooting streaks are long enough to indicate that he has hot hands? We can compare his streak lengths to someone without hot hands: an independent shooter. 

%

\subsection*{Simulations in R}
While we don't have any data from a shooter we know to have independent shots, that sort of data is very easy to simulate in R. In a simulation, you set the ground rules of a random process and then the computer uses random numbers to generate an outcome that adheres to those rules. As a simple example, you can simulate flipping a fair coin with the following.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{outcomes} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"heads"}\hlstd{,} \hlstr{"tails"}\hlstd{)}

\hlkwd{sample}\hlstd{(outcomes,} \hlkwc{size} \hlstd{=} \hlnum{1}\hlstd{,} \hlkwc{replace} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


The vector \hlstd{outcomes} can be thought of as a hat with two slips of paper in it: one slip says ``heads" and the other says ``tails". The function \hlkwd{sample} draws one slip from the hat and tells us if it was a head or a tail. 

Run the second command listed above several times. Just like when flipping a coin, sometimes you'll get a heads, sometimes you'll get a tails, but in the long run, you'd expect to get roughly equal numbers of each.

If you wanted to simulate flipping a fair coin 100 times, you could either run the function 100 times or, more simply, adjust the \hlkwc{size} argument, which governs how many samples to draw (the \hlkwc{replace = }\hlnum{TRUE} argument indicates we put the slip of paper back in the hat before drawing again). Save the resulting vector of heads and tails in a new object called \hlstd{sim\_fair\_coin}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sim_fair_coin} \hlkwb{<-} \hlkwd{sample}\hlstd{(outcomes,} \hlkwc{size} \hlstd{=} \hlnum{100}\hlstd{,} \hlkwc{replace} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


To view the results of this simulation, type the name of the object and then use \hlkwd{table} to count up the number of heads and tails.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sim_fair_coin}

\hlkwd{table}\hlstd{(sim_fair_coin)}
\end{alltt}
\end{kframe}
\end{knitrout}


Since there are only two elements in \hlstd{outcomes}, the probability that we ``flip'' a coin and it lands heads is 0.5. Say we're trying to simulate an unfair coin that we know only lands heads 20\% of the time. We can adjust for this by adding an argument called \hlkwc{prob}, which provides a vector of two probability weights.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sim_unfair_coin} \hlkwb{<-} \hlkwd{sample}\hlstd{(outcomes,} \hlkwc{size} \hlstd{=} \hlnum{100}\hlstd{,} \hlkwc{replace} \hlstd{=} \hlnum{TRUE}\hlstd{,} \hlkwc{prob} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{0.2}\hlstd{,}
    \hlnum{0.8}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}


\hlkwc{prob}\hlkwc{=}\hlkwd{c}\hlnum{(0.2,0.8)} indicates that for the two elements in the \hlstd{outcomes} vector, we want to select the first one, \hlstr{heads}, with probability 0.2 and the second one, \hlstr{tails} with probability 0.8.\symbolfootnote[2]{Another way of thinking about this is to think of the outcome space as a bag of 10 chips, where 2 chips are labeled ``head" and 8 chips ``tail". Therefore at each draw, the probability of drawing a chip that says ``head" is 20\%, and ``tail" is 80\%.}

\begin{exercise}
In your simulation of flipping the unfair coin 100 times, how many flips came up heads?
\end{exercise}

In a sense, we've shrunken the size of the slip of paper that says ``heads'', making it less likely to be drawn and we've increased the size of the slip of paper saying ``tails'', making it more likely to be drawn. When we simulated the fair coin, both slips of paper were the same size. This happens by default if you don't provide a \hlkwc{prob} argument; all elements in the \hlstd{outcomes} vector have an equal probability of being drawn.

If you want to learn more about \hlkwd{sample} or any other function, recall that you can always check out its help file.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlopt{?}\hlstd{sample}
\end{alltt}
\end{kframe}
\end{knitrout}


%

\subsection*{Simulating the Independent Shooter}
Simulating a basketball player who has independent shots uses the same mechanism that we use to simulate a coin flip. To simulate a single shot from an independent shooter with a shooting percentage of 50\% we type,

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{outcomes} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"H"}\hlstd{,} \hlstr{"M"}\hlstd{)}

\hlstd{sim_basket} \hlkwb{<-} \hlkwd{sample}\hlstd{(outcomes,} \hlkwc{size} \hlstd{=} \hlnum{1}\hlstd{,} \hlkwc{replace} \hlstd{=} \hlnum{TRUE}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


To make a valid comparison between Kobe and our simulated independent shooter, we need to align both their shooting percentage and the number of attempted shots.

\begin{exercise}
What change needs to be made to the \hlkwd{sample} function so that it reflects a shooting percentage of 45\%? Make this adjustment, then run a simulation to sample 133 shots. Assign the output of this simulation to a new object called \hlstd{sim\_basket}.
\end{exercise}

Note that we've named the new vector \hlstd{sim\_basket}, the same name that we gave to the previous vector reflecting a shooting percentage of 50\%. In this situation, R overwrites the old object with the new one, so always make sure that you don't need the information in an old vector before reassigning its name.

With the results of the simulation saved as \hlstd{sim\_basket}, we have the data necessary to compare Kobe to our independent shooter. We can look at Kobe's data alongside our simulated data.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{kobe}\hlopt{$}\hlstd{basket}

\hlstd{sim_basket}
\end{alltt}
\end{kframe}
\end{knitrout}


Both data sets represent the results of 133 shot attempts, each with the same shooting percentage of 45\%. We know that our simulated data is from a shooter that has independent shots. That is, we know the simulated shooter does not have a hot hand.

\vspace{2cm}

%

\section*{On your own}
\subsection*{Comparing Kobe Bryant to the Independent Shooter}
Using \hlkwd{calc\_streak}, compute the streak lengths of \hlstd{sim\_basket}.

\begin{enumerate}
\item Describe the distribution of streak lengths. What is the typical streak length for this simulated independent shooter with a 45\% shooting percentage? How long is the player's longest streak of baskets in 133 shots?

\item If you were to run the simulation of the independent shooter a second time, how would you expect its streak distribution to compare to the distribution from the question above? Exactly the same? Somewhat similar? Totally different? Explain your reasoning.

\item How does Kobe Bryant's distribution of streak lengths from page~\pageref{kobeStreak} compare to the distribution of streak lengths for the simulated shooter? Using this comparison, do you have evidence that the hot hand model fits Kobe's shooting patterns? Explain.

\item What concepts from the textbook are covered in this lab? What concepts, if any, are not covered in the textbook? Have you seen these concepts elsewhere, e.g. lecture, discussion section, previous labs, or homework problems? Be specific in your answer.
\end{enumerate}

\end{document}
